#include "drv_uart.h"


void init_uart(T_UWORD  baud_rate){
    TXSTA = 0;
    RCSTA = 0;
    /*Auto-Baud Acquisition Rollover Status bit*/
    ABDOVF = 0;
    /*Received Data Polarity Select bit*/
    RXDTP = RX_NOT_INV;
    /*Clock and Data Polarity Select bit*/
    TXCKP = TX_NOT_INV;
    /*High Speed*/
    BRGH = 0;
    /*Configuracion Baud Rate*/
    /*16-Bit Baud Rate Register Enable bit*/
    BRG16 = 0;  //Modo de Baud Rate en 8 bit
    SPBRGH = 0;
    SPBRG = (T_UBYTE)((((SYS_FREQ_M*1000.0)/((float)baud_rate/100.0))*10.0)/64.0 - 1); //Calculo de Baud Rate
    /*Wake-up Enable bit*/
    WUE = 0;    //EUSART will continue to sample the RX pin ? interrupt generated on falling edge; bit cleared in hardware on following rising edge
    /*Auto-Baud Detect Enable bit*/
    ABDEN = AUTO_BAUDRATE_DISABLE;
    /*Configure TRANSMIT STATUS AND CONTROL REGISTER*/
    TXSTA = TX_ENABLE;  //Enable TX
    /*Configure RCSTA: RECEIVE STATUS AND CONTROL REGISTER*/
    RCSTA = (SPEN_ENABLE | CONT_RX); //Enable RX and continuos receive
}

void uart_send_char(T_UBYTE lub_data){
    TXREG = lub_data;
}

static T_UBYTE n = 0;
T_UBYTE uart_print(char lc_DataString[]){
    if(lc_DataString[n] != 0){
       TXREG = lc_DataString[n];
       n++;
       return(0);
    }
    else{
        n = 0;
        return(1);
    }
}

T_UBYTE get_uart(){
    return RCREG;
}